package com.navinfo.platform.aero.data.rest.core.util;

/**
 * com.navinfo.location.rprest.util
 *
 * @author zhangdong
 * @date 2017/12/27
 */
public class StringUtils {
    private StringUtils() {
    }

    public static String before(String s, char c) {
        int pos = s.indexOf(c);
        if (pos < 0) {
            throw new IllegalArgumentException("String '" + s + "' must contains '" + c + "'.");
        } else {
            return s.substring(0, pos);
        }
    }

    public static String after(String s, char c) {
        int pos = s.indexOf(c);
        if (pos < 0) {
            throw new IllegalArgumentException("String '" + s + "' must contains '" + c + "'.");
        } else {
            return s.substring(pos + 1);
        }
    }

    public static String afterLast(String s, char c) {
        int pos = s.lastIndexOf(c);
        if (pos < 0) {
            throw new IllegalArgumentException("String '" + s + "' must contains '" + c + "'.");
        } else {
            return s.substring(pos + 1);
        }
    }

    public static String[] splitLast(String s, char c) {
        int pos = s.lastIndexOf(c);
        return pos < 0 ? null : new String[]{s.substring(0, pos), s.substring(pos + 1)};
    }

    public static String[] splitLast(String s, String delimiter) {
        int pos = s.lastIndexOf(delimiter);
        return pos < 0 ? null : new String[]{s.substring(0, pos), s.substring(pos + delimiter.length())};
    }

    public static boolean isNull(String s) {
        return s == null || "".equals(s) || s.equalsIgnoreCase("null");
    }

}
